package com.hualan.sfzy.service.content.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.content.Article;
import com.hualan.sfzy.mapper.content.ArticleMapper;
import com.hualan.sfzy.mapper.score.ScoreMapper;
import com.hualan.sfzy.service.content.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper mapper;
    @Autowired
    private ScoreMapper scoreMapper;
    public  int add(Article article){

        Date date=new Date();
        article.setCreateTime(date);
        return mapper.insert(article);
    }
    public int remove(Integer id){
        return mapper.delete(id);
    }
    public int removeBatch(Integer[] ids){
        return mapper.deleteBatch(ids);
    }
    public int edit(Article article){
        return mapper.update(article);
    }
    @Transactional
    public Article findById(Integer id){
       Article article=new Article();
       article.setId(id);
        Article article1= mapper.selectById(id);
        Integer readNumber=article1.getReadNumber();

       article.setReadNumber(++readNumber);
        mapper.update(article);
        return mapper.selectById(id);
    }
    public List<Article> find(){
        return mapper.select(null);
    }

    public Map<String,Object> findByPage(Integer pageNum, Integer pageSize, Article article){
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list = mapper.select(article);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        Map<String , Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }
    public Map<String, Object> findByPageStatus(Integer pageNum, Integer pageSize, Article entity){
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list = mapper.selectByStatus(entity);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        Map<String , Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

}



